package doyyin.cookie;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class TtWid {
    private static String PARAMS_HEADERS = "{\n" +
            "    \"Accept\": \"*/*\",\n" +
            "    \"Accept-Encoding\": \"*/*\",\n" +
            "    \"Content-Type\": \"text/plain;charset=UTF-8\",\n" +
            "    \"Referer\": \"https://www.douyin.com/?recommend=1\",\n" +
            "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36\"\n" +
            "}";
    private static final String API = "https://ttwid.bytedance.com/ttwid/union/register/";
    private static final String DATA = "{\"region\":\"cn\",\"aid\":1768,\"needFid\":false,\"service\":\"www.ixigua.com\",\"migrate_info\":{\"ticket\":\"\",\"source\":\"node\"},\"cbUrlProtocol\":\"https\",\"union\":true}";


    public static void main(String[] args) throws IOException {
        HttpPost httpPost = new HttpPost(API);
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {

            // 设置请求方法
            JSONObject jsonObject = JSONObject.parseObject(PARAMS_HEADERS);
            for (String key : jsonObject.keySet()) {
                httpPost.setHeader(key, jsonObject.getString(key));
            }
            httpPost.setEntity(new StringEntity(DATA));

            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                Header[] allHeaders = response.getAllHeaders();
                for (Header allHeader : allHeaders) {
                    System.out.println(allHeader.getName()+"     "+ allHeader.getValue());
                }
                String result = EntityUtils.toString(response.getEntity());
                System.out.println(result);
            }
        }
    }
}
